home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994…tember: Reference Library / Dev.CD Sep 94.toast / Periodicals / develop / develop Issue 19 / develop 19 code / SimpliFace_V2 / Sources / PascalString.cp < prev    next >
Encoding:
Text File  |  1994-04-15  |  17.7 KB  |  643 lines  |  [TEXT/MPS ]

  1. //----------------------------------------------------------------------------------------
  2. // PascalString.cp 
  3. // Copyright © 1985-1992 by Apple Computer, Inc.  All rights reserved.
  4. //----------------------------------------------------------------------------------------
  5.  
  6.  
  7. #include "PascalString.h"
  8.  
  9. #ifndef __STDIO__
  10. #include <StdIo.h>
  11. #endif
  12.  
  13.  
  14. #pragma segment Main
  15.  
  16.  
  17. //========================================================================================
  18. // CLASS CString
  19. //========================================================================================
  20.  
  21. CString::CString()
  22. {
  23. }
  24.  
  25. CString::~CString()
  26. {
  27. }
  28.  
  29.  
  30. CStr255::CStr255()
  31. {
  32. }
  33.  
  34. CStr255::~CStr255()
  35. {
  36. }
  37.  
  38.  
  39. CStr63::CStr63()
  40. {
  41. }
  42.  
  43. CStr63::~CStr63()
  44. {
  45. }
  46.  
  47. CStr32::CStr32()
  48. {
  49. }
  50.  
  51. CStr32::~CStr32()
  52. {
  53. }
  54.  
  55. CStr31::CStr31()
  56. {
  57. }
  58.  
  59. CStr31::~CStr31()
  60. {
  61. }
  62.  
  63.  
  64.  
  65. //----------------------------------------------------------------------------------------
  66. // CString::InsertHelper(CString):
  67. //----------------------------------------------------------------------------------------
  68.  
  69. void CString::InsertHelper(const CString& insStr,
  70.                           short pos,
  71.                           short maxLength)
  72. {
  73.     if (pos > Length() + 1)
  74.     {
  75. #if qDebugMsg
  76.         fprintf(stderr, "###CString::InsertHelper: Insert position greater than length of CString.\n");
  77. #endif
  78.         if (Length() < maxLength)    
  79.             pos = Length() + 1;
  80.     }
  81.     
  82. #if qDebugMsg
  83.     if (Length() + insStr.Length() > maxLength)
  84.         fprintf(stderr, "### CString::InsertHelper: CString truncated during insert call.\n");    
  85. #endif
  86.  
  87.     short usableLengthOfInsertString;
  88.     short endPosOfInsertString;
  89.     short usableLengthOfShiftedString;
  90.     
  91.     if (pos + insStr.Length() > maxLength)
  92.         usableLengthOfInsertString = maxLength - pos + 1;
  93.     else
  94.         usableLengthOfInsertString = insStr.Length();
  95.     endPosOfInsertString = pos + usableLengthOfInsertString - 1;
  96.     
  97.     if ((endPosOfInsertString + 1) + (Length() - pos + 1) > maxLength)
  98.         usableLengthOfShiftedString = maxLength - endPosOfInsertString;
  99.     else
  100.         usableLengthOfShiftedString = Length() - pos + 1;
  101.         
  102.     memmove(&fStr[endPosOfInsertString + 1], &fStr[pos], usableLengthOfShiftedString);
  103.     memmove(&fStr[pos], &insStr.fStr[1], usableLengthOfInsertString);
  104.     Length() = usableLengthOfShiftedString + endPosOfInsertString;
  105. } // CString::InsertHelper(CString)
  106.  
  107.  
  108. //----------------------------------------------------------------------------------------
  109. // CString::InsertHelper(char*):
  110. //----------------------------------------------------------------------------------------
  111.  
  112. void CString::InsertHelper(const char* insStr,
  113.                           short pos,
  114.                           short maxLength)
  115. {
  116.     this->InsertHelper(CStr255(insStr), pos, maxLength);
  117. } // CString::InsertHelper(char*)
  118.  
  119.  
  120. //----------------------------------------------------------------------------------------
  121. // CString::operator[]: !!! we'd ideally like this method to be inlined but CFront doesn't
  122. // seem to want to inline it for us !!!
  123. //----------------------------------------------------------------------------------------
  124.  
  125. unsigned char& CString::operator[](short pos)
  126. {
  127.     return fStr[pos];
  128. } // CString::operator[] for non-const CString
  129.  
  130.  
  131. //----------------------------------------------------------------------------------------
  132. // CString::operator char*:
  133. //----------------------------------------------------------------------------------------
  134.  
  135. CString::operator char*() const
  136. {
  137.     const short kTempCStrings = 4;
  138.     static short currentCString = 0;
  139.     static char cStrings[kTempCStrings][kStr255Len+1];
  140.     
  141.     currentCString = (currentCString + 1) % kTempCStrings;
  142.     
  143.     strncpy(cStrings[currentCString], (char *) &fStr[1], Length());
  144.     cStrings[currentCString][Length()] = '\0';
  145.     
  146.     return cStrings[currentCString];
  147. } // CString::operator char*
  148.  
  149. CString::operator const unsigned char*() const
  150. {
  151.      return (const unsigned char *) this;
  152. }
  153.  
  154.  
  155. //----------------------------------------------------------------------------------------
  156. // CString::operator long:
  157. //----------------------------------------------------------------------------------------
  158.  
  159. CString::operator long() const
  160. {
  161.     // The following statement looks like it should work. Right?
  162.     //
  163.     //    return *((long *) &fStr[1]);
  164.     //
  165.     // Wrong, the C compiler generates a MOVE.L starting on a odd byte boundary for the
  166.     // preceding statement. This is illegal on the 68000. But its _NOT_ a bug, because
  167.     // according to the ANSI C reference manual, "A pointer to one type may be converted
  168.     // to a pointer to another type. The resulting pointer may cause an addressing
  169.     // exception if the subject pointer does not refer to an object suitably aligned in
  170.     // storage".
  171.     
  172.     long returnLong;
  173.     
  174.     memcpy(&returnLong, &fStr[1], sizeof(long));
  175.     return returnLong;
  176. } // CString::operator long
  177.  
  178.  
  179. //----------------------------------------------------------------------------------------
  180. // CString::Pos(char*):
  181. //----------------------------------------------------------------------------------------
  182.  
  183. unsigned char CString::Pos(const char* subStr, unsigned char startPos)
  184. {
  185.     char cStr[kStr255Len + 1];
  186.     char* ptr;
  187.     
  188.     memcpy(cStr, &fStr[1], Length());
  189.     cStr[Length()] = 0;
  190.     ptr = strstr(&cStr[startPos - 1], subStr);
  191.     return ptr != NULL ? (ptr - cStr) + 1 : 0;
  192. } // CString::Pos(char*)
  193.  
  194.  
  195. //----------------------------------------------------------------------------------------
  196. // CString::Pos(CString):
  197. //----------------------------------------------------------------------------------------
  198.  
  199. unsigned char CString::Pos(const CString& subStr, unsigned char startPos)
  200. {
  201.     char cStr[kStr255Len + 1];
  202.  
  203.     memcpy(cStr, &subStr.fStr[1], subStr.Length());
  204.     cStr[subStr.Length()] = 0;
  205.     return this->Pos(cStr, startPos);
  206. } // CString::Pos(CString)
  207.  
  208.  
  209.  
  210. //========================================================================================
  211. // CLASS CStr255
  212. //========================================================================================
  213.  
  214.  
  215. //----------------------------------------------------------------------------------------
  216. // CStr255::CStr255(char*):
  217. //----------------------------------------------------------------------------------------
  218.  
  219. CStr255::CStr255(const char* str)
  220. {
  221.     // Truncate the C CString to 255 bytes if necessary.
  222.  
  223.     Length() = str == NULL ? 0 : strlen(str);
  224.     
  225.     if (Length() > kStr255Len)
  226.         Length() = kStr255Len;
  227.     memcpy(&fStr[1], str, Length());
  228. } // CStr255::CStr255(char*)
  229.  
  230.  
  231. //----------------------------------------------------------------------------------------
  232. // CStr255::CStr255(long): Useful for converting OSType's into CStr255's.
  233. //----------------------------------------------------------------------------------------
  234.  
  235. CStr255::CStr255(const long id)
  236. {
  237.     Length() = 4;
  238.     memcpy(&fStr[1], &id, Length());
  239. } // CStr255::CStr255(long)
  240.  
  241.  
  242. //----------------------------------------------------------------------------------------
  243. // CStr255::Copy:
  244. //----------------------------------------------------------------------------------------
  245.  
  246. CStr255 CStr255::Copy(short pos, short length)
  247. {
  248.     CStr255 newString;
  249.     
  250.     length = length > Length() - pos + kLengthByte ? Length() - pos + kLengthByte : length;
  251.     
  252.     if (length > 0)
  253.     {
  254.         memcpy(&newString.fStr[1], &fStr[pos], length);
  255.         newString.Length() = length;
  256.     }
  257.     else
  258.         newString = "";
  259.         
  260.     return newString;
  261.     
  262. } // CStr255::Copy
  263.  
  264.  
  265. //----------------------------------------------------------------------------------------
  266. // CStr255::operator+:
  267. //----------------------------------------------------------------------------------------
  268.  
  269. CStr255 operator+(const CString& s1,
  270.                   const char* s2)
  271. {
  272.     CStr255 newStr;
  273.     short s2Len = s2 == NULL ? 0 : strlen((const char *) s2);
  274.  
  275.     if (s1.Length() + s2Len > kStr255Len)
  276.         newStr.Length() = kStr255Len;
  277.     else
  278.         newStr.Length() = s1.Length() + s2Len;
  279.         
  280.     memcpy(&newStr.fStr[1], &s1.fStr[1], s1.Length());
  281.     memcpy(&newStr.fStr[s1.Length() + kLengthByte], s2, newStr.Length() - s1.Length());
  282.  
  283.     return newStr;
  284. } // CStr255::operator+
  285.  
  286.  
  287. //----------------------------------------------------------------------------------------
  288. // CStr255::operator+(char*,CString):
  289. //----------------------------------------------------------------------------------------
  290.  
  291. CStr255 operator+(const char* s1,
  292.                   const CString& s2)
  293. {
  294.     CStr255 newStr;
  295.     short s1Len = s1 == NULL ? 0 : strlen((const char *) s1);
  296.  
  297.     if (s1Len + s2.Length() > kStr255Len)
  298.         newStr.Length() = kStr255Len;
  299.     else
  300.         newStr.Length() = s1Len + s2.Length();
  301.         
  302.     memcpy(&newStr.fStr[1], s1, s1Len);
  303.     memcpy(&newStr.fStr[s1Len + kLengthByte], s2.fStr + 1, newStr.Length() - s1Len);
  304.  
  305.     return newStr;
  306. } // CStr255::operator+(char*,CString)
  307.  
  308.  
  309. //----------------------------------------------------------------------------------------
  310. // CStr255::operator+(CString,CString):
  311. //----------------------------------------------------------------------------------------
  312.  
  313. CStr255 operator+(const CString& s1,
  314.                   const CString& s2)
  315. {
  316.     CStr255 newStr;
  317.  
  318.     if (s1.Length() + s2.Length() > kStr255Len)
  319.         newStr.Length() = kStr255Len;
  320.     else
  321.         newStr.Length() = s1.Length() + s2.Length();
  322.         
  323.     memcpy(&newStr.fStr[1], &s1.fStr[1], s1.Length());
  324.     memcpy(&newStr.fStr[s1.Length() + kLengthByte], s2.fStr + 1, newStr.Length() - s1.Length());
  325.  
  326.     return newStr;
  327. } // CStr255::operator+(CString,CString)
  328.  
  329.  
  330. //----------------------------------------------------------------------------------------
  331. // CStr255::operator +=(CString):  Concatinate a string
  332. //----------------------------------------------------------------------------------------
  333.  
  334. CStr255& CStr255::operator += (const CString& str)
  335. {
  336.     InsertHelper (str, Length() + 1, kStr255Len);
  337.     return *this;
  338. } // CStr255::operator +=(CString)
  339.  
  340.  
  341. //----------------------------------------------------------------------------------------
  342. // CStr255::operator +=(char*):  Concatinate a string
  343. //----------------------------------------------------------------------------------------
  344.  
  345. CStr255& CStr255::operator += (const char* str)
  346. {
  347.     InsertHelper (str, Length() + 1, kStr255Len);
  348.     return *this;
  349. } // CStr255::operator +=(char*)
  350.  
  351.  
  352. //----------------------------------------------------------------------------------------
  353. // CStr255::operator +=(char):  Concatinate a single character
  354. //----------------------------------------------------------------------------------------
  355.  
  356. CStr255& CStr255::operator += (const char ch)
  357. {
  358.     if (++Length() <= kStr255Len)
  359.         fStr[Length()] = ch;
  360.     else
  361.     {
  362.         --Length();
  363. #if qDebugMsg
  364.         fprintf(stderr, "###CStr255::operator+=: Concatenation produces CStr255 overflow.\n");
  365. #endif
  366.     }
  367.     
  368.     return *this;
  369. } // CStr255::operator +=(char)
  370.  
  371.  
  372. //----------------------------------------------------------------------------------------
  373. // CStr255::operator =:
  374. //----------------------------------------------------------------------------------------
  375.  
  376. CStr255& CStr255::operator = (const char* str)
  377. {
  378.     if (str)
  379.     {
  380.         // Truncate the C CString to 255 bytes if necessary.
  381.         register size_t itsSize = strlen(str);
  382.         if (itsSize > kStr255Len)
  383.             Length() = kStr255Len;
  384.         else
  385.             Length() = itsSize;
  386.  
  387.         memcpy(&fStr[1], str, Length());
  388.     }
  389.     else
  390.         Length() = 0;
  391.     
  392.     return *this;
  393. } // CStr255::operator =
  394.  
  395.  
  396.  
  397. //========================================================================================
  398. // CLASS CStr63
  399. //========================================================================================
  400.  
  401.  
  402. //----------------------------------------------------------------------------------------
  403. // CStr63::CStr63(char*):
  404. //----------------------------------------------------------------------------------------
  405.  
  406. CStr63::CStr63(const char* str)
  407. {
  408.     // Truncate the C CString to 63 bytes if necessary.
  409.  
  410.     Length() = str == NULL ? 0 : strlen((const char*)str);
  411.     if (Length() > kStr63Len)
  412.         Length() = kStr63Len;
  413.     memcpy(&fStr[1], str, Length());
  414. } // CStr63::CStr63(char*)
  415.  
  416.  
  417. //----------------------------------------------------------------------------------------
  418. // CStr63::CStr63(long):
  419. //----------------------------------------------------------------------------------------
  420.  
  421. CStr63::CStr63(const long id)
  422. {
  423.     Length() = 4;
  424.     memcpy(&fStr[1], &id, Length());
  425. } // CStr63::CStr63(long)
  426.  
  427.  
  428. //----------------------------------------------------------------------------------------
  429. // CStr63::Copy:
  430. //----------------------------------------------------------------------------------------
  431.  
  432. CStr63 CStr63::Copy(short pos, short length)
  433. {
  434.     CStr63 newString;
  435.     
  436.     length = length > Length() - pos + kLengthByte ? Length() - pos + kLengthByte : length;
  437.     
  438.     if (length > 0)
  439.     {
  440.         memcpy(&newString.fStr[1], &fStr[pos], length);
  441.         newString.Length() = length;
  442.     }
  443.     else
  444.         newString = "";
  445.         
  446.     return newString;
  447.     
  448. } // CStr63::Copy
  449.  
  450.  
  451. //----------------------------------------------------------------------------------------
  452. // CStr63::operator +=(CString):  Concatinate a string
  453. //----------------------------------------------------------------------------------------
  454.  
  455. CStr63& CStr63::operator += (const CString& str)
  456. {
  457.     InsertHelper (str, Length() + 1, kStr63Len);
  458.     return *this;
  459. } // CStr63::operator +=(CString)
  460.  
  461.  
  462. //----------------------------------------------------------------------------------------
  463. // CStr63::operator +=(char*):  Concatinate a string
  464. //----------------------------------------------------------------------------------------
  465.  
  466. CStr63& CStr63::operator += (const char* str)
  467. {
  468.     InsertHelper (str, Length() + 1, kStr63Len);
  469.     return *this;
  470. } // CStr63::operator +=(char*)
  471.  
  472.  
  473. //----------------------------------------------------------------------------------------
  474. // CStr63::operator +=(char):  Concatinate a single character
  475. //----------------------------------------------------------------------------------------
  476.  
  477. CStr63& CStr63::operator += (const char ch)
  478. {
  479.     if (++Length() <= kStr63Len)
  480.         fStr[Length()] = ch;
  481.     else
  482.     {
  483.         --Length();
  484. #if qDebugMsg
  485.         fprintf(stderr, "###CStr63::operator+=: Concatenation produces CStr63 overflow.\n");
  486. #endif
  487.     }
  488.     
  489.     return *this;
  490. } // CStr63::operator +=(char)
  491.  
  492.  
  493.  
  494. //========================================================================================
  495. // CLASS CStr32
  496. //========================================================================================
  497.  
  498.  
  499. //----------------------------------------------------------------------------------------
  500. // CStr32::CStr32(char*):
  501. //----------------------------------------------------------------------------------------
  502.  
  503. CStr32::CStr32(const char* str)
  504. {
  505.     // Truncate the C CString to 32 bytes if necessary.
  506.  
  507.     Length() = str == NULL ? 0 : strlen((const char*)str);
  508.     if (Length() > kStr32Len)
  509.         Length() = kStr32Len;
  510.     memcpy(&fStr[1], str, Length());
  511. } // CStr32::CStr32(char*)
  512.  
  513.  
  514. //----------------------------------------------------------------------------------------
  515. // CStr32::CStr32(long):
  516. //----------------------------------------------------------------------------------------
  517.  
  518. CStr32::CStr32(const long id)
  519. {
  520.     Length() = 4;
  521.     memcpy(&fStr[1], &id, Length());
  522. } // CStr32::CStr32(long)
  523.  
  524.  
  525. //----------------------------------------------------------------------------------------
  526. // CStr32::Copy:
  527. //----------------------------------------------------------------------------------------
  528.  
  529. CStr32 CStr32::Copy(short pos, short length)
  530. {
  531.     CStr32 newString;
  532.     
  533.     length = length > Length() - pos + kLengthByte ? Length() - pos + kLengthByte : length;
  534.     
  535.     if (length > 0)
  536.     {
  537.         memcpy(&newString.fStr[1], &fStr[pos], length);
  538.         newString.Length() = length;
  539.     }
  540.     else
  541.         newString = "";
  542.         
  543.     return newString;
  544.  
  545. } // CStr32::Copy
  546.  
  547.  
  548.  
  549. //========================================================================================
  550. // CLASS CStr31
  551. //========================================================================================
  552.  
  553.  
  554. //----------------------------------------------------------------------------------------
  555. // CStr31::CStr31(char*):
  556. //----------------------------------------------------------------------------------------
  557.  
  558. CStr31::CStr31(const char* str)
  559. {
  560.     // Truncate the C CString to 31 bytes if necessary.
  561.  
  562.     Length() = str == NULL ? 0 : strlen((const char*)str);
  563.     if (Length() > kStr31Len)
  564.         Length() = kStr31Len;
  565.     memcpy(&fStr[1], str, Length());
  566. } // CStr31::CStr31(char*)
  567.  
  568.  
  569. //----------------------------------------------------------------------------------------
  570. // CStr31::CStr31(long):
  571. //----------------------------------------------------------------------------------------
  572.  
  573. CStr31::CStr31(const long id)
  574. {
  575.     Length() = 4;
  576.     memcpy(&fStr[1], &id, Length());
  577. } // CStr31::CStr31(long)
  578.  
  579.  
  580. //----------------------------------------------------------------------------------------
  581. // CStr31::Copy:
  582. //----------------------------------------------------------------------------------------
  583.  
  584. CStr31 CStr31::Copy(short pos, short length)
  585. {
  586.     CStr31 newString;
  587.     
  588.     length = length > Length() - pos + kLengthByte ? Length() - pos + kLengthByte : length;
  589.     
  590.     if (length > 0)
  591.     {
  592.         memcpy(&newString.fStr[1], &fStr[pos], length);
  593.         newString.Length() = length;
  594.     }
  595.     else
  596.         newString = "";
  597.         
  598.     return newString;
  599.  
  600. } // CStr31::Copy
  601.  
  602.  
  603. //----------------------------------------------------------------------------------------
  604. // CStr31::operator +=(CString):  Concatinate a string
  605. //----------------------------------------------------------------------------------------
  606.  
  607. CStr31& CStr31::operator += (const CString& str)
  608. {
  609.     InsertHelper (str, Length() + 1, kStr31Len);
  610.     return *this;
  611. } // CStr31::operator +=(CString)
  612.  
  613.  
  614. //----------------------------------------------------------------------------------------
  615. // CStr31::operator +=(char*):  Concatinate a string
  616. //----------------------------------------------------------------------------------------
  617.  
  618. CStr31& CStr31::operator += (const char* str)
  619. {
  620.     InsertHelper (str, Length() + 1, kStr31Len);
  621.     return *this;
  622. } // CStr31::operator +=(char*)
  623.  
  624.  
  625. //----------------------------------------------------------------------------------------
  626. // CStr31::operator +=(char):  Concatinate a single character
  627. //----------------------------------------------------------------------------------------
  628.  
  629. CStr31& CStr31::operator += (const char ch)
  630. {
  631.     if (++Length() <= kStr31Len)
  632.         fStr[Length()] = ch;
  633.     else
  634.     {
  635.         --Length();
  636. #if qDebugMsg
  637.         fprintf(stderr,"###CStr31::operator+=: Concatenation produces CStr31 overflow.\n");
  638. #endif
  639.     }
  640.     
  641.     return *this;
  642. } // CStr31::operator +=(char)
  643.